package com.qyhl.webtv.commonlib.utils.router;

import android.content.Context;
import android.util.Log;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.service.UserService;

/**
 * 登陆拦截器
 *
 * @author helin
 * @date 2019年2月28日16:43:29
 */
@Interceptor(priority = 8, name = "login")
public class LoginInterceptor implements IInterceptor {

    private Context mContext;

    @Autowired(name = ServicePathConstant.USER_SERICE)
    UserService userService;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if (postcard.getExtra() == ARouterPathConstant.LOGIN_NEEDED) {
            userService.checkUserLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        Log.e("inter", "已经登陆");
                        callback.onContinue(postcard);
                    } else {
                        Log.e("inter", "未登陆");
                        callback.onInterrupt(null);
                    }
                }

                @Override
                public void error(String s) {
                    callback.onInterrupt(null);
                }
            });

        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        mContext = context;
        //注解初始化
        ARouter.getInstance().inject(this);
        Log.e("inter", "拦截器初始化");
    }
}
